﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class AlternateSkuBLL : IBaseInfoDetail
    {
        private static readonly IAlternateSkuDA alternateDA = DataAccess.CreateAlternateSkuDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IPackDA iPackDA = DataAccess.CreatePackDA();
        private static readonly IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();

        public AlternateSkuBLL()
        {
            this.pubParasInfo = null;
        }

        public AlternateSkuBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public AlternateSkuBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string AlternateSkuImportByData(string[][] ImportByData, string IsUpdate)
        {
            Exception ex;
            string SkuIDTrue = string.Empty;
            string SkuIDFalse = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 1; ImportByData.Length > i; i++)
                {
                    string CS00004;
                    AlternateSkuInfo alternateSkuInfo = new AlternateSkuInfo();
                    for (int n = 0; ImportByData[i].Length > n; n++)
                    {
                        try
                        {
                            if (!string.IsNullOrEmpty(ImportByData[i][n]))
                            {
                                switch (ImportByData[0][n])
                                {
                                    case "货主代码":
                                    {
                                        alternateSkuInfo.OwnerID = ImportByData[i][n];
                                        continue;
                                    }
                                    case "货品代码":
                                    {
                                        alternateSkuInfo.SkuID = ImportByData[i][n];
                                        continue;
                                    }
                                    case "别名":
                                    {
                                        alternateSkuInfo.AltSkuID = ImportByData[i][n];
                                        continue;
                                    }
                                    case "包装":
                                    {
                                        alternateSkuInfo.PackID = ImportByData[i][n];
                                        continue;
                                    }
                                    case "类型":
                                    {
                                        alternateSkuInfo.Type = ImportByData[i][n];
                                        continue;
                                    }
                                    case "供应商":
                                    {
                                        alternateSkuInfo.VendorID = ImportByData[i][n];
                                        continue;
                                    }
                                    case "默认计量单位":
                                    {
                                        alternateSkuInfo.DefaultUomID = ImportByData[i][n];
                                        continue;
                                    }
                                    case "自定义01":
                                    {
                                        alternateSkuInfo.Udf1 = ImportByData[i][n];
                                        continue;
                                    }
                                    case "自定义02":
                                    {
                                        alternateSkuInfo.Udf2 = ImportByData[i][n];
                                        continue;
                                    }
                                    case "自定义03":
                                    {
                                        alternateSkuInfo.Udf3 = ImportByData[i][n];
                                        continue;
                                    }
                                    case "自定义04":
                                    {
                                        alternateSkuInfo.Udf4 = ImportByData[i][n];
                                        continue;
                                    }
                                    case "自定义05":
                                    {
                                        alternateSkuInfo.Udf5 = ImportByData[i][n];
                                        continue;
                                    }
                                    case "自定义06":
                                    {
                                        alternateSkuInfo.Udf6 = ImportByData[i][n];
                                        continue;
                                    }
                                    case "自定义07":
                                    {
                                        alternateSkuInfo.Udf7 = ImportByData[i][n];
                                        continue;
                                    }
                                    case "自定义08":
                                    {
                                        alternateSkuInfo.Udf8 = ImportByData[i][n];
                                        continue;
                                    }
                                    case "自定义09":
                                    {
                                        alternateSkuInfo.Udf9 = ImportByData[i][n];
                                        continue;
                                    }
                                    case "自定义10":
                                    {
                                        alternateSkuInfo.Udf10 = ImportByData[i][n];
                                        continue;
                                    }
                                    case "创建人":
                                    {
                                        alternateSkuInfo.CreatedBy = ImportByData[i][n];
                                        continue;
                                    }
                                }
                            }
                        }
                        catch (Exception exception1)
                        {
                            ex = exception1;
                            if (!string.IsNullOrEmpty(alternateSkuInfo.SkuID))
                            {
                                CS00004 = SkuIDFalse;
                                SkuIDFalse = CS00004 + alternateSkuInfo.SkuID + "(数据内容异常,导入失败);" + ex.Message + "<br/>";
                            }
                        }
                    }
                    alternateSkuInfo.IsActive = "Y";
                    alternateSkuInfo.UpdatedBy = alternateSkuInfo.CreatedBy;
                    if (string.IsNullOrEmpty(alternateSkuInfo.PackID))
                    {
                        alternateSkuInfo.PackID = "STD";
                    }
                    if (!string.IsNullOrEmpty(alternateSkuInfo.SkuID))
                    {
                        try
                        {
                            if (!ownerDA.CheckTradePartnerIDUnique(dataBase, tran, alternateSkuInfo.OwnerID, this.dataBaseID[0]))
                            {
                                CS00004 = SkuIDFalse;
                                SkuIDFalse = CS00004 + alternateSkuInfo.SkuID + "(数据的货主 " + alternateSkuInfo.OwnerID + " 不存在);<br/>";
                            }
                            else if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, alternateSkuInfo.OwnerID))
                            {
                                CS00004 = SkuIDFalse;
                                SkuIDFalse = CS00004 + alternateSkuInfo.SkuID + "(数据的货主 " + alternateSkuInfo.OwnerID + " 未授权);<br/>";
                            }
                            else if (!iPackDA.CheckPackIDUnique(dataBase, tran, alternateSkuInfo.PackID, this.dataBaseID[0]))
                            {
                                CS00004 = SkuIDFalse;
                                SkuIDFalse = CS00004 + alternateSkuInfo.SkuID + "(数据包装 " + alternateSkuInfo.PackID + " 不存在);<br/>";
                            }
                            else if (alternateDA.CheckAlternateSkuIDUnique(dataBase, tran, alternateSkuInfo.OwnerID, alternateSkuInfo.SkuID, alternateSkuInfo.AltSkuID, alternateSkuInfo.VendorID, this.dataBaseID[0]))
                            {
                                if (IsUpdate == "Y")
                                {
                                    if (alternateDA.UpdateAlternateSku(dataBase, tran, alternateSkuInfo, this.dataBaseID) > 0)
                                    {
                                        SkuIDFalse = SkuIDFalse + alternateSkuInfo.SkuID + "(数据已经更新);<br/>";
                                    }
                                }
                                else
                                {
                                    SkuIDFalse = SkuIDFalse + alternateSkuInfo.SkuID + "(数据已经存在,导入失败);<br/>";
                                }
                            }
                            else
                            {
                                alternateSkuInfo.CreatedBy = this.pubParasInfo.UserID;
                                alternateSkuInfo.CreatedDate = DateTime.Now;
                                alternateSkuInfo.UpdatedBy = this.pubParasInfo.UserID;
                                alternateSkuInfo.UpdatedDate = DateTime.Now;
                                alternateSkuInfo.DefaultUomID = string.IsNullOrEmpty(alternateSkuInfo.DefaultUomID) ? "" : alternateSkuInfo.DefaultUomID;
                                alternateSkuInfo.Descr = string.IsNullOrEmpty(alternateSkuInfo.Descr) ? "" : alternateSkuInfo.Descr;
                                alternateSkuInfo.IsActive = "Y";
                                alternateSkuInfo.PackID = string.IsNullOrEmpty(alternateSkuInfo.PackID) ? "" : alternateSkuInfo.PackID;
                                alternateSkuInfo.Type = string.IsNullOrEmpty(alternateSkuInfo.Type) ? "" : alternateSkuInfo.Type;
                                alternateSkuInfo.VendorID = string.IsNullOrEmpty(alternateSkuInfo.VendorID) ? "" : alternateSkuInfo.VendorID;
                                alternateSkuInfo.Udf1 = string.IsNullOrEmpty(alternateSkuInfo.Udf1) ? "" : alternateSkuInfo.Udf1;
                                alternateSkuInfo.Udf10 = string.IsNullOrEmpty(alternateSkuInfo.Udf10) ? "" : alternateSkuInfo.Udf10;
                                alternateSkuInfo.Udf2 = string.IsNullOrEmpty(alternateSkuInfo.Udf2) ? "" : alternateSkuInfo.Udf2;
                                alternateSkuInfo.Udf3 = string.IsNullOrEmpty(alternateSkuInfo.Udf3) ? "" : alternateSkuInfo.Udf3;
                                alternateSkuInfo.Udf4 = string.IsNullOrEmpty(alternateSkuInfo.Udf4) ? "" : alternateSkuInfo.Udf4;
                                alternateSkuInfo.Udf5 = string.IsNullOrEmpty(alternateSkuInfo.Udf5) ? "" : alternateSkuInfo.Udf5;
                                alternateSkuInfo.Udf6 = string.IsNullOrEmpty(alternateSkuInfo.Udf6) ? "" : alternateSkuInfo.Udf6;
                                alternateSkuInfo.Udf7 = string.IsNullOrEmpty(alternateSkuInfo.Udf7) ? "" : alternateSkuInfo.Udf7;
                                alternateSkuInfo.Udf8 = string.IsNullOrEmpty(alternateSkuInfo.Udf8) ? "" : alternateSkuInfo.Udf8;
                                alternateSkuInfo.Udf9 = string.IsNullOrEmpty(alternateSkuInfo.Udf9) ? "" : alternateSkuInfo.Udf9;
                                if (alternateDA.InsertAlternateSku(dataBase, tran, alternateSkuInfo, this.dataBaseID) > 0)
                                {
                                    SkuIDTrue = SkuIDTrue + alternateSkuInfo.SkuID + ";";
                                }
                            }
                        }
                        catch (Exception exception2)
                        {
                            ex = exception2;
                            if (!string.IsNullOrEmpty(alternateSkuInfo.SkuID))
                            {
                                CS00004 = SkuIDFalse;
                                SkuIDFalse = CS00004 + alternateSkuInfo.SkuID + "(数据交互时出现异常,导入失败);" + ex.Message + "<br/>";
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception exception3)
            {
                ex = exception3;
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return SkuIDFalse;
        }

        public string DeleteDetail(List<string> ownerID, List<string> skuID, List<string> alternateSkuID)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (ownerID.Count != skuID.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2210_002", this.pubParasInfo.UserLanguage);
                }
                List<AlternateSkuInfo> alternateSkuInfos = new List<AlternateSkuInfo>();
                AlternateSkuInfo alternateSkuInfo = null;
                foreach (string id in alternateSkuID)
                {
                    string[] alterSkuID_VendorID = id.Split(new char[] { '\r' });
                    alternateSkuInfo = alternateDA.GetAlternateSkuByID(dataBase, tran, ownerID[0], skuID[0], alterSkuID_VendorID[0], alterSkuID_VendorID[1], this.dataBaseID[0]);
                    alternateSkuInfos.Add(alternateSkuInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (alternateDA.DeleteAlternateSku(dataBase, tran, ownerID, skuID, alternateSkuID, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < ownerID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<AlternateSkuInfo>(dataBase, tran, alternateSkuInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ALTERNATE_SKU", this.dataBaseID[0]);
                        string CS00004 = temp;
                        temp = CS00004 + ownerID[i] + "," + skuID[i] + "," + alternateSkuID[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return alternateDA.GetAllAlternateSku(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            AlternateSkuQueryEntity alternateSkuQuery = obj as AlternateSkuQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(alternateSkuQuery.SkuID))
            {
                sqlWhere.Add(" SKU_ID " + alternateSkuQuery.SkuID);
            }
            if (!string.IsNullOrEmpty(alternateSkuQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + alternateSkuQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(alternateSkuQuery.AltSkuID))
            {
                sqlWhere.Add(" ALT_SKU_ID " + alternateSkuQuery.AltSkuID);
            }
            if (!string.IsNullOrEmpty(alternateSkuQuery.VendorID))
            {
                sqlWhere.Add(" VENDOR_ID " + alternateSkuQuery.VendorID);
            }
            return alternateDA.GetAlternateSkuByQueryList(sqlWhere, alternateSkuQuery, this.dataBaseID[0]);
        }

        public object GetDetailInfoByID(string ownerID, string skuID, string alternateSkuID)
        {
            string[] altSkuID_vendorID = alternateSkuID.Split(new char[] { '\r' });
            if (altSkuID_vendorID.Length == 1)
            {
                return alternateDA.GetAlternateSkuByID(ownerID, skuID, altSkuID_vendorID[0], "", this.dataBaseID[0]);
            }
            return alternateDA.GetAlternateSkuByID(ownerID, skuID, altSkuID_vendorID[0], altSkuID_vendorID[1], this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string ownerID, string skuID)
        {
            List<AlternateSkuInfo> alternateSkuInfos = alternateDA.GetAlternateSkuListByID(ownerID, skuID, this.dataBaseID[0]);
            List<object> objs = null;
            if ((alternateSkuInfos != null) && (alternateSkuInfos.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < alternateSkuInfos.Count; i++)
                {
                    objs.Add(alternateSkuInfos[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            AlternateSkuInfo alternateSkuInfo = obj as AlternateSkuInfo;
            string alternateSkuID = string.Empty;
            try
            {
                if (alternateSkuInfo == null)
                {
                    return alternateSkuID;
                }
                if (alternateDA.CheckAlternateSkuIDUnique(alternateSkuInfo.OwnerID, alternateSkuInfo.SkuID, alternateSkuInfo.AltSkuID, alternateSkuInfo.VendorID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2210_001", this.pubParasInfo.UserLanguage, new List<string> { alternateSkuInfo.AltSkuID });
                }
                alternateSkuInfo.UpdatedBy = alternateSkuInfo.CreatedBy;
                alternateSkuInfo.CreatedDate = DateTime.Now;
                alternateSkuInfo.UpdatedDate = DateTime.Now;
                if (alternateDA.InsertAlternateSku(alternateSkuInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<AlternateSkuInfo>(null, alternateSkuInfo, AuditAction.Insert, this.pubParasInfo.UserID, "ALTERNATE_SKU", this.dataBaseID[0]);
                    alternateSkuID = alternateSkuInfo.AltSkuID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return alternateSkuID;
        }

        public string UpdateDetail(object obj)
        {
            string alternateSkuID = string.Empty;
            try
            {
                AlternateSkuInfo alternateSkuInfo = obj as AlternateSkuInfo;
                AlternateSkuInfo oldAlternateSkuInfo = alternateDA.GetAlternateSkuByID(alternateSkuInfo.OwnerID, alternateSkuInfo.SkuID, alternateSkuInfo.AltSkuID, alternateSkuInfo.VendorID, this.dataBaseID[0]);
                if (alternateDA.UpdateAlternateSku(alternateSkuInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<AlternateSkuInfo>(oldAlternateSkuInfo, alternateSkuInfo, AuditAction.Update, this.pubParasInfo.UserID, "ALTERNATE_SKU", this.dataBaseID[0]);
                    alternateSkuID = alternateSkuInfo.AltSkuID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return alternateSkuID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

